<template>
  <div class="user-profile">
    <van-nav-bar
      title="个人中心"
      class="page-nav-bar"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="fileRef" @change="onFileChange" />
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image class="avatar" :src="user.photo" round fit="cover" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpDataNameShow = true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpDataGenderShow = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpDataBirthdayShow = true"
    />
    <!-- 修改头像弹出层 -->
    <van-popup
      v-model="isUpDataPhotoShow"
      position="bottom"
      style="height: 100%"
    >
      <update-photo @update-photo="user.photo = $event" :img="img" @close="isUpDataPhotoShow = false" v-if="isUpDataPhotoShow"></update-photo>
    </van-popup>

    <!-- 编辑昵称弹出层 -->
    <van-popup
      v-model="isUpDataNameShow"
      position="bottom"
      style="height: 100%"
    >
      <update-name
        v-model="user.name"
        @close="isUpDataNameShow = false"
        v-if="isUpDataNameShow"
      />
    </van-popup>

    <!-- 修改性别弹出层 -->
    <van-popup v-model="isUpDataGenderShow" position="bottom">
      <update-gender
        v-model="user.gender"
        @close="isUpDataGenderShow = false"
        v-if="isUpDataGenderShow"
      />
    </van-popup>

    <!-- 修改生日弹出层 -->
    <van-popup v-model="isUpDataBirthdayShow" position="bottom">
      <update-birthday
        v-model="user.birthday"
        @close="isUpDataBirthdayShow = false"
        v-if="isUpDataBirthdayShow"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUSerProfile } from '@/api/user'
// 修改昵称组件
import UpdateName from './components/update-name.vue'
// 修改性别组件
import UpdateGender from './components/update-gender.vue'
// 修改生日组件
import UpdateBirthday from './components/update-birthday.vue'

import UpdatePhoto from './components/update-photo'
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto

  },
  data () {
    return {
      user: {},
      isUpDataNameShow: false,
      isUpDataGenderShow: false,
      isUpDataBirthdayShow: false,
      isUpDataPhotoShow: false,
      img: undefined
    }
  },
  created () {
    this.getUserProfile()
  },
  methods: {
    async getUserProfile () {
      try {
        const { data: { data } } = await getUSerProfile()
        this.user = data
      } catch (error) {
        console.error(error)
        this.$toast('获取数据失败!')
      }
    },
    onFileChange () {
      const file = this.$refs.fileRef.files[0]
      this.img = URL.createObjectURL(file)
      this.isUpDataPhotoShow = true
      this.$refs.fileRef.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.avatar {
  width: 60px;
  height: 60px;
}
.van-popup {
  background-color: #f5f7f9;
}
</style>
